﻿/*
 * Licensed under the MIT license.
 * 
 * Copyright © 2010 Mikko Väänänen
 */

using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;

namespace ShellTail {

	class Program {

		static void Main(string[] args) {

			if (args.Length == 0) {
				PrintUsage();
				return;
			}

			string path = args[0];

			IConsoleWriter writer = CreateConsoleWriter(args);
			FileReader reader = new FileReader(path, writer);
			reader.Read();
		}

		private static IConsoleWriter CreateConsoleWriter(string[] args) {
			string pattern = args.Length > 1 ? args[1] : null;

			if (pattern != null) {
				return new HighlightingConsoleWriter(pattern);
			} else {
				return new SimpleConsoleWriter();
			}
		}

		static void PrintUsage() {
			String usage =
				"Command line utility to monitor a log file.\n\n" +
				"Press CTRL+C to stop monitoring." +
				"\n\n" +
				"STAIL [file] [regexp]\n" +
				"\n" +
				"  [file]\n" +
				"              Specifies file to monitor.\n" +
				"  [regexp]\n" +
				"              (Optional) Highlight matching rows.\n" +
				"\n" +
				"Example: stail server.log ERROR\n";

			Console.WriteLine(usage);
		}

	}
}
